package cn.selfiot.iotman.view.projectitem;

import cn.selfiot.iotman.core.I18N;
import cn.selfiot.iotman.core.StageManager;
import cn.selfiot.iotman.core.StageParams;
import cn.selfiot.iotman.helper.MessageDialogHelper;
import cn.selfiot.iotman.helper.ProjectPersistHelper;
import cn.selfiot.iotman.model.mqtt.ProjectModel;
import cn.selfiot.iotman.view.ResourceConstants;
import cn.selfiot.iotman.view.project.MqttProjectController;
import cn.selfiot.iotman.view.workbench.MqttWorkbenchController;
import com.panemu.tiwulfx.dialog.MessageDialog;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URL;
import java.util.ResourceBundle;

/**
 * 项目卡片视图组件
 *
 * @author hugo
 * @since 2020/7/30
 */
public class ProjectItemController implements Initializable {
    private static final Logger LOGGER = LoggerFactory.getLogger(ProjectItemController.class);
    
    @FXML
    public VBox project;
    @FXML
    public ImageView imgType;
    @FXML
    public Button btnEdit;
    @FXML
    public Button btnRemove;
    @FXML
    public VBox vbContent;
    @FXML
    public Label host;
    @FXML
    public Label url;
    
    private StageManager stageManager;
    private ResourceBundle bundle;
    private ProjectModel mqttProject;
    private Stage primaryStage;
    
    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        stageManager = StageManager.getInstance();
        bundle = I18N.getBundle();
        if (mqttProject != null) {
            boolean enableSSL = mqttProject.getSsl() != null
                    && mqttProject.getSsl().getEnable();
            String protocol = enableSSL ? "ssl://" : "tcp://";
            String hostUrl = protocol + mqttProject.getHostAddr() + ":"
                    + mqttProject.getHostPort();
            this.host.setText(mqttProject.getProjectName());
            this.url.setText(hostUrl);
        }
        
        btnRemove.setOnAction(
                actionEvent -> removeProject(mqttProject)
        );
        btnEdit.setOnAction(
                actionEvent -> editProject(mqttProject)
        );
        vbContent.setOnMouseClicked(
                actionEvent -> openWorkbench(mqttProject)
        );
    }
    
    public void setPrimaryStage(Stage primaryStage) {
        this.primaryStage = primaryStage;
    }
    
    public void openWorkbench(ProjectModel projectModel) {
        StageParams params = new StageParams();
        params.set("project", projectModel);
        MqttWorkbenchController controller = new MqttWorkbenchController();
        controller.setStage(primaryStage);
        controller.setParams(params);
        this.stageManager.loadStage(
                primaryStage,
                controller,
                ResourceConstants.MQTT_WORKBENCH_VIEW,
                ResourceConstants.MQTT_WORKBENCH_RES,
                I18N.get("mqtt.workbench.title"),
                null
        ).show();
    }
    
    public void editProject(ProjectModel projectModel) {
        StageParams params = new StageParams();
        params.set("project", projectModel);
        
        Stage stage = new Stage();
        MqttProjectController controller = new MqttProjectController();
        controller.setStage(stage);
        controller.setParams(params);
        this.stageManager.loadStage(
                stage,
                controller,
                ResourceConstants.PROJECT_VIEW,
                ResourceConstants.PROJECT_RES,
                I18N.get("project.title"),
                Modality.APPLICATION_MODAL
        );
        stage.show();
    }
    
    private void removeProject(ProjectModel projectModel) {
        MessageDialog.Answer answer = MessageDialogHelper.confirm(
                this.primaryStage
                , bundle.getString("dialog.del.message"));
        if (answer == MessageDialog.Answer.YES_OK) {
            ProjectPersistHelper.remove(projectModel);
            this.stageManager.callback(ResourceConstants.MAIN_VIEW);
        }
    }
    
    public void setMqttProject(ProjectModel mqttProject) {
        this.mqttProject = mqttProject;
    }
}
